package com.tanhua.server;

import cn.hutool.core.collection.CollUtil;
import com.itheima.commons.utils.Constants;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 根据环信id查询用户详情
     */
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1、根据环信id查询用户
        User user = userApi.findByHuanXinId(huanxinId);
        //2、根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.getById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    public void contactsTo(Long userId) {
        Long id = UserHolder.getUserId();

        //向环信平台添加账号连接
        Boolean contact = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + id, Constants.HX_USER_PREFIX + userId);
        if (!contact) {
            throw new BusinessException(ErrorResult.contentError());
        }
        //保存数据到Friend表
        Boolean aBoolean = friendApi.contacts(id, userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.contentError());
        }
    }

    /**
     * 查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult getContacts(Integer page, Integer pagesize, String keyword) {
        //查询所有好友Id
        List<Friend> friends = friendApi.getByUserId(UserHolder.getUserId(), page, pagesize);
        List<Long> ids = CollUtil.getFieldValues(friends, "friendId", Long.class);

        //创建用户详情过滤条件
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> userInfoMap = userInfoApi.getByIds(ids, userInfo);

        List<ContactVo> contactVos = ids.stream().map((item) -> {
            UserInfo info = userInfoMap.get(item);
            return ContactVo.init(info);
        }).collect(Collectors.toList());

        return new PageResult(page, pagesize, 0L, contactVos);
    }

    public void unContactsTo(Long likeUserId) {
        //删除Friend表的好友关系
        friendApi.unContactsTo(UserHolder.getUserId(), likeUserId);
        //删除环信好友关系
        Boolean aBoolean = huanXinTemplate.deleteContact(
                Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + likeUserId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.contentError());
        }

    }
}
